# -*- coding: utf-8 -*-
# author: sixmillions
# date: 2022/3/24

# 身份运算符
# is   is not

######
# 值类型比较
#####
a = 1
b = 1
print(1, a is b)  # True
print(2, a is not b)  # False
# 内存地址
print(3, id(a), id(b))
a1 = 1.0
# 比较值
print(4, a == a1)  # True
# 比较内存
print(5, a is a1)  # False

c = 'STR'
d = 'STR'
print(6, c is d)  # True
print(7, c is not d)  # False
# 内存地址
print(8, id(c), id(d))

e = (333, 444)
f = (333, 444)
print(9, e is f)  # True
print(10, e is not f)  # False
# 内存地址
print(11, id(e), id(f))
f1 = (444, 333)
print(12, e is f1)  # False

print("-----------------------")
######
# 引用类型比较
#####
g = [1, 2, 3]
h = [1, 2, 3]
print(13, g is h)  # False
print(14, g is not h)  # True
print(15, id(g), id(h))

i = {1, 2, 3}
j = {1, 2, 3}
print(16, i is j)  # False
print(17, i is not j)  # True
print(18, i == j)  # True
print(19, id(i), id(j))
j1 = {3, 2, 1}
print(20, i is j1)  # False
print(21, i == j1)  # True

k = {'key1': 111, 'key2': 222}
l = {'key1': 111, 'key2': 222}
print(22, k is l)  # False
print(23, k is not l)  # True
print(24, k == l)  # True
l1 = {'key2': 222, 'key1': 111}
print(25, k is l1)  # False
print(26, k == l1)  # True

#####
# 类型判断
####
# 不推荐
print(27, type(a) == int)  # True
print(28, type(i) == set)  # True
# 用isinstance
print(29, isinstance(a, int))  # True
print(30, isinstance(i, set))  # True
print(31, isinstance(h, (int, str, set)))  # False


# type()不会认为子类是一种父类类型。
# isinstance()会认为子类是一种父类类型。
class A:
    pass


class B(A):
    pass


print(32, isinstance(A(), A))  # True
print(33, type(A()) == A)  # True
print(34, isinstance(B(), A))  # True
print(35, type(B()) == A)  # False
